package com.tebiecloud.yunst.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.yunst.client.model.entity.YunAgentPay;
import com.tebiecloud.yunst.server.service.YunAgentPayService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 *  前端控制器
 * @author tebie
 * @date 2019-09-07
 */
@RestController
@RequestMapping("/yunagentpay")
public class YunAgentPayController {

    @Autowired
    private YunAgentPayService yunAgentPayService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<YunAgentPay>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        YunAgentPay query = pageParams.mapToObject(YunAgentPay.class);
        QueryWrapper<YunAgentPay> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(yunAgentPayService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<YunAgentPay> get(@RequestParam("id") Long id) {
        YunAgentPay entity = yunAgentPayService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param bizOrderNo 代付单号,
     * @param collectPayList 源代收订单列表[{"amount":1,"bizOrderNo":"TGDSS1565680600877"}],
     * @param recieverId 收款人id,
     * @param amount 金额（单位：分）,
     * @param payStatus 代付状态：0未付，1已付，2失败,
     * @param remark 
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "bizOrderNo", required = false) String bizOrderNo,
        @RequestParam(value = "collectPayList", required = false) String collectPayList,
        @RequestParam(value = "recieverId", required = false) String recieverId,
        @RequestParam(value = "amount", required = false) Integer amount,
        @RequestParam(value = "payStatus", required = false) Integer payStatus,
        @RequestParam(value = "remark", required = false) String remark
    ) {
        YunAgentPay entity = new YunAgentPay();
        entity.setBizOrderNo(bizOrderNo);
        entity.setCollectPayList(collectPayList);
        entity.setRecieverId(recieverId);
        entity.setAmount(amount);
        entity.setPayStatus(payStatus);
        entity.setRemark(remark);
        yunAgentPayService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param bizOrderNo 代付单号
     * @param collectPayList 源代收订单列表[{"amount":1,"bizOrderNo":"TGDSS1565680600877"}]
     * @param recieverId 收款人id
     * @param amount 金额（单位：分）
     * @param payStatus 代付状态：0未付，1已付，2失败
     * @param remark 
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "agentPayId", required = false) Long agentPayId,
            @RequestParam(value = "bizOrderNo", required = false) String bizOrderNo,
            @RequestParam(value = "collectPayList", required = false) String collectPayList,
            @RequestParam(value = "recieverId", required = false) String recieverId,
            @RequestParam(value = "amount", required = false) Integer amount,
            @RequestParam(value = "payStatus", required = false) Integer payStatus,
            @RequestParam(value = "remark", required = false) String remark
    ) {
        YunAgentPay entity = new YunAgentPay();
        entity.setAgentPayId(agentPayId);
        entity.setBizOrderNo(bizOrderNo);
        entity.setCollectPayList(collectPayList);
        entity.setRecieverId(recieverId);
        entity.setAmount(amount);
        entity.setPayStatus(payStatus);
        entity.setRemark(remark);
        yunAgentPayService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            yunAgentPayService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            yunAgentPayService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
